package me.ezjs.web.controller.staff;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.ezjs.core.model.FlipFilter;
import me.ezjs.core.model.Operate;
import me.ezjs.core.model.Page;
import me.ezjs.core.web.util.Result;
import me.ezjs.core.web.web.controller.BaseAction;
import me.ezjs.staff.manager.StaffManager;
import me.ezjs.staff.model.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by junli on 2017/8/21.
 */
@Api(description = "用户表 代码示例")
@RestController
@RequestMapping(value = "staff")
public class StaffController extends BaseAction {

    private StaffManager staffManager;

    @Autowired
    public StaffController(StaffManager staffManager) {
        this.staffManager = staffManager;
    }

    @ApiOperation(value = "查询列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false,
                    paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false,
                    paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "flip", method = RequestMethod.POST)
    public Result flip(
            @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo
    ) {
        FlipFilter flipFilter = new FlipFilter(Staff.class);
        flipFilter.addSearch("%" + name + "%", Operate.LIKE, "name");
        flipFilter.updatePageNo(pageNo);
        flipFilter.addSort("name", true);
        Page page = staffManager.flipListInPage(flipFilter);
        return Result.ok(page);
    }

    @ApiOperation(value = "新增用户", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true,
                    paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "age", value = "年龄", required = false,
                    paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public Result edit(
            @ApiIgnore Staff staff
    ) {
        staffManager.save(staff);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户信息", notes = "多个ID使用','分隔")
    @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "query", dataType = "string")
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public Result remove(
            @RequestParam(value = "ids", required = true) String ids
    ) {
        String idArr[] = ids.split(",");
        FlipFilter flipFilter = new FlipFilter(Staff.class);
        flipFilter.addSearch(java.util.Arrays.asList(idArr), Operate.IN, "id");
        staffManager.remove(flipFilter);
        return Result.ok();
    }

    @ApiOperation(value = "根据ID查询详情", notes = "")
    @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public Result detail(
            @PathVariable("id") Integer id
    ) {
        Staff staff = staffManager.get(id);
        return Result.ok(staff);
    }
}
